package com.visizen.wisdomclass.server.client;

import com.visizen.wisdomclass.server.model.WcDiscussAttach;
import com.visizen.wisdomclass.server.model.WcTopicTop;
import com.visizen.wisdomclass.server.model.WcTopicTopExample;

import java.util.List;

import com.visizen.wisdomclass.server.model.dto.DiscussExt;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

public interface WcTopicTopMapper {
    @Update({
            "update wc_topic_top",
            "set create_time = #{create_time,jdbcType=TIMESTAMP},",
            "expiration_time = #{expiration_time,jdbcType=TIMESTAMP}",
            "where topic_id = #{topic_id,jdbcType=BIGINT} and discuss_id=#{discuss_id,jdbcType=BIGINT}"
    })
    int updateByTopicAndDiscuss(WcTopicTop record);

    /**
     * 获取指定的讨论
     *
     * @param topicId
     * @param start
     * @param end
     * @return
     */
    @Select(" SELECT\n" +
            "  d.*,u.attach_id,u.fullname,u.area_id\n" +
            " FROM wc_discuss as d,wc_user as u\n" +
            " WHERE d.discuss_id IN (SELECT\n" +
            "                       discuss_id\n" +
            "                     FROM wc_topic_top\n" +
            "                     WHERE topic_id = #{topicId} and expiration_time > current_time) and u.user_id = d.user_id " +
            "  order by discuss_time desc" +
            " LIMIT #{start},#{end}")
    @Results({
            @Result(property = "discuss_id", column = "discuss_id"),
            @Result(property = "userAttachId", column = "attach_id"),
            @Result(property = "userFullname", column = "fullname"),
            @Result(property = "userAreaId", column = "area_id"),
            @Result(property = "attachList", column = "discuss_id", javaType = List.class, many = @Many(select = "selectAttaches")),
            @Result(property = "commentCount", column = "discuss_id", one = @One(select = "selectCommentCount")),
            @Result(property = "praiseCount", column = "discuss_id", one = @One(select = "selectPraiseCount")),
            @Result(property = "seeCount", column = "discuss_id", one = @One(select = "selectSeeCount"))
    })
    List<DiscussExt> selectTopDiscuss(@Param("topicId") Long topicId,
                                      @Param("start") int start,
                                      @Param("end") int end);

    @Select("select * from wc_topic_top where topic_id=#{topicId} and discuss_id=#{discussId}")
    WcTopicTop selectByTopicAndDiscuss(@Param("topicId") Long topicId,
                                       @Param("discussId") Long discussId);

    /**
     * 讨论附件列表
     *
     * @param discussId
     * @return
     */
    @Select("select * from wc_discuss_attach where discuss_id=#{discussId}")
    List<WcDiscussAttach> selectAttaches(@Param("discussId") Long discussId);

    /**
     * 讨论的评论数
     *
     * @param discussId
     * @return
     */
    @Select("select COUNT(*) from wc_discuss_comment where discuss_id=#{discussId}")
    int selectCommentCount(@Param("discussId") Long discussId);

    /**
     * 讨论的赞数量
     *
     * @param discussId
     * @return
     */
    @Select("select COUNT(*) from wc_discuss_praise where discuss_id=#{discussId}")
    int selectPraiseCount(@Param("discussId") Long discussId);

    /**
     * 讨论的查看数量
     *
     * @param discussId
     * @return
     */
    @Select("select COUNT(*) from wc_discuss_log_see where discuss_id=#{discussId}")
    int selectSeeCount(@Param("discussId") Long discussId);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_top
     *
     * @mbggenerated
     */
    @SelectProvider(type = WcTopicTopSqlProvider.class, method = "countByExample")
    int countByExample(WcTopicTopExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_top
     *
     * @mbggenerated
     */
    @DeleteProvider(type = WcTopicTopSqlProvider.class, method = "deleteByExample")
    int deleteByExample(WcTopicTopExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_top
     *
     * @mbggenerated
     */
    @Delete({
            "delete from wc_topic_top",
            "where topic_top_id = #{topic_top_id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long topic_top_id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_top
     *
     * @mbggenerated
     */
    @Insert({
            "insert into wc_topic_top (topic_top_id, topic_id, ",
            "discuss_id, create_time, ",
            "expiration_time)",
            "values (#{topic_top_id,jdbcType=BIGINT}, #{topic_id,jdbcType=BIGINT}, ",
            "#{discuss_id,jdbcType=BIGINT}, #{create_time,jdbcType=TIMESTAMP}, ",
            "#{expiration_time,jdbcType=TIMESTAMP})"
    })
    int insert(WcTopicTop record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_top
     *
     * @mbggenerated
     */
    @InsertProvider(type = WcTopicTopSqlProvider.class, method = "insertSelective")
    int insertSelective(WcTopicTop record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_top
     *
     * @mbggenerated
     */
    @SelectProvider(type = WcTopicTopSqlProvider.class, method = "selectByExample")
    @Results({
            @Result(column = "topic_top_id", property = "topic_top_id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "topic_id", property = "topic_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "discuss_id", property = "discuss_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "create_time", property = "create_time", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "expiration_time", property = "expiration_time", jdbcType = JdbcType.TIMESTAMP)
    })
    List<WcTopicTop> selectByExample(WcTopicTopExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_top
     *
     * @mbggenerated
     */
    @Select({
            "select",
            "topic_top_id, topic_id, discuss_id, create_time, expiration_time",
            "from wc_topic_top",
            "where topic_top_id = #{topic_top_id,jdbcType=BIGINT}"
    })
    @Results({
            @Result(column = "topic_top_id", property = "topic_top_id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "topic_id", property = "topic_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "discuss_id", property = "discuss_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "create_time", property = "create_time", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "expiration_time", property = "expiration_time", jdbcType = JdbcType.TIMESTAMP)
    })
    WcTopicTop selectByPrimaryKey(Long topic_top_id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_top
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcTopicTopSqlProvider.class, method = "updateByExampleSelective")
    int updateByExampleSelective(@Param("record") WcTopicTop record, @Param("example") WcTopicTopExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_top
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcTopicTopSqlProvider.class, method = "updateByExample")
    int updateByExample(@Param("record") WcTopicTop record, @Param("example") WcTopicTopExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_top
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcTopicTopSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(WcTopicTop record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_top
     *
     * @mbggenerated
     */
    @Update({
            "update wc_topic_top",
            "set topic_id = #{topic_id,jdbcType=BIGINT},",
            "discuss_id = #{discuss_id,jdbcType=BIGINT},",
            "create_time = #{create_time,jdbcType=TIMESTAMP},",
            "expiration_time = #{expiration_time,jdbcType=TIMESTAMP}",
            "where topic_top_id = #{topic_top_id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(WcTopicTop record);
}